import { Observable } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { Component, OnInit, ElementRef } from '@angular/core';
import { Router, ActivatedRoute, ParamMap } from '@angular/router';
import * as XLSX from 'xlsx';
import { ImportedFileService } from '../imported-file.service';
import { Importedfile, ImageItemDetail, ScreenItemDescriptionEntity, DesignEntity, ItemControlEntity } from '../imported-file';
import { HttpClient, HttpHeaders } from '@angular/common/http';

@Component({
  selector: 'app-image-item-detail',
  templateUrl: './image-item-detail.component.html',
  styleUrls: ['./image-item-detail.component.css'],
})
export class ImageItemDetailComponent implements OnInit {
  imageItemDetaillist$: ScreenItemDescriptionEntity[];
  importedFile$: Observable<DesignEntity>;
  selectedId: number;
  screenId: string;
  designTypeId: string;
  designName: string;
  screenName: string;
  errList$: String[];
  itemControlList$: ItemControlEntity[] = null;
  errmsgOfItemControl: String = null;

  constructor(
    private service: ImportedFileService,
    private router: Router,
    private route: ActivatedRoute
  ) {}

  ngOnInit() {

    this.route.queryParams.subscribe(params => {
      this.screenId = params.screenId;
      this.designTypeId = params.designTypeId;
      this.designName = params.designName;
      this.screenName = params.screenName;
    });
    
    this.service.getImageitemDetails(this.screenId, this.designTypeId).subscribe(
      data => {this.imageItemDetaillist$ = data; 
        if(this.imageItemDetaillist$.length === 0){
          return;
        }
      }
    );

  }

  integrityCheck() {
    //check2のエラーを消す
    this.imageItemDetaillist$.forEach((item)=>item.checkFlg2=false);
    //check3のエラーを消す
    this.errmsgOfItemControl = null;
    this.itemControlList$ = null;

    this.service.integrityCheck(this.imageItemDetaillist$).subscribe(
      data => {this.imageItemDetaillist$ = data; 
        if(this.imageItemDetaillist$.length === 0){
          return;
        }
      }
    );
    // this.service.integrityCheck(this.imageItemDetaillist$);
  }

  integrityCheck2() {

    //check1のエラーを消す
    this.imageItemDetaillist$.forEach((item)=>item.checkFlg=false);
    //check3のエラーを消す
    this.errmsgOfItemControl = null;
    this.itemControlList$ = null;

    this.service.integrityCheck2(this.imageItemDetaillist$).subscribe(
      data => {this.imageItemDetaillist$ = data; 
        if(this.errList$.length === 0){
          return;
        }
      }
    );
  }

  integrityCheck3() {
    //check1のエラーを消す
    this.imageItemDetaillist$.forEach((item)=>item.checkFlg=false);
     //check2のエラーを消す
     this.imageItemDetaillist$.forEach((item)=>item.checkFlg2=false);

    this.service.integrityCheck3(this.imageItemDetaillist$).subscribe(
      data => {this.itemControlList$ = data; 
        if(this.itemControlList$.length === 0){
          return;
        }
        this.errmsgOfItemControl = "エラーが発生しましたが、赤色の項目の所在行をクリックして、詳細エラーを確認してください。";
      }
    );
  }

  isErrorOfItemControl(imageItemDetail:ScreenItemDescriptionEntity){
    if(this.itemControlList$ === null){
      return false;
    }

    var errConditionControl =this.itemControlList$.filter(function(item){
      return item.screenId == imageItemDetail.screenId && item.section == imageItemDetail.section; 
    });

    if(errConditionControl.length === 0){
      return false;
    }else{
      return true;
    }
  }

  getErrMsgOfItemControl(imageItemDetail:ScreenItemDescriptionEntity){
    
    var errItemControlList = this.itemControlList$.filter((item) => item.screenId == imageItemDetail.screenId && item.section == imageItemDetail.section);

    if(errItemControlList.length === 0){
      this.errmsgOfItemControl = "エラーが発生しましたが、赤色の項目の所在行をクリックして、詳細エラーを確認してください。";
      //no error found
      return;
    }else{
      this.errmsgOfItemControl = "項番"+errItemControlList[0].section +":全部の権限制御が「×」か「●」になりますが、条件制御の";
    　var errorConditionNo:string = "";
      for(let errConditionControl of errItemControlList){
        if(errorConditionNo != ""){
          errorConditionNo = errorConditionNo + ","
        }
        errorConditionNo =errorConditionNo + errConditionControl.controlLevel1 + "-" + errConditionControl.controlLevel2;
      }

      this.errmsgOfItemControl = this.errmsgOfItemControl + errorConditionNo + "の中に「〇」があります。"
    }
  }

  back(importedfile: DesignEntity) {
    // TODO
    this.router.navigate(['/importedfiles']);
  }

  
}

